﻿using System.Web.Mvc;
using System.Web.Routing;
using FluentValidation.Attributes;
using FluentValidation.Mvc;
using Quickie.Models;
using Quickie.Models.IoC;
using Quickie.Models.Data;
using Quickie.Models.Entities;
using Quickie.Models.ViewModel;
using Quickie.Models.Interfaces;

namespace Quickie
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",
                "{controller}/{action}",
                new { controller = "Login", action = "Index", id = "" },
                new string[] { "Quickie.Controllers" });
            
            routes.MapRoute(
                "Product",
                "product/{category}/{product}/{id}",
                new { controller = "Product", action = "Details" },
                new { id = @"\d+" },
                new string[] { "Quickie.Controllers" });

            routes.MapRoute(
                "Category",
                "product/{name}/{id}",
                new { controller = "Product", action = "ByCategory"},
                new { id = @"\d+" },
                new string[] { "Quickie.Controllers" });

            routes.MapRoute(
               "Person",
               "person/{company}/{person}/{id}",
               new { controller = "Person", action = "Details" },
               new { id = @"\d+" },
               new string[] { "Quickie.Controllers" });

            routes.MapRoute(
                "Company",
                "person/{name}/{id}",
                new { controller = "Person", action = "ByCompany" },
                new { id = @"\d+" },
                new string[] { "Quickie.Controllers" });

            routes.MapRoute(
                "ProductImageAdmin",
                "productadmin/getimage/{productID}",
                 new { controller = "ProductAdmin", action = "GetImage", productID = "" }
               );

            routes.MapRoute(
                "PersonImageAdmin",
                "personadmin/getimage/{personID}",
                 new { controller = "PersonAdmin", action = "GetImage", personID = "" }
               );

            routes.MapRoute(
                "TaskDelete",
                "task/{action}/{ID}",
                 new { controller = "Task", action = "Delete", ID = "" }
               );

            routes.MapRoute(
                "TaskEdit",
                "task/{action}/{ID}",
                 new { controller = "Task", action = "Edit", ID = "" }
               );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);

            ContainerBootstrapper.BootstrappStructureMap();
            ControllerBuilder.Current.SetControllerFactory(new ControllerFactory());
            ModelBinders.Binders.DefaultBinder = new FluentValidationModelBinder(new AttributedValidatorFactory());
            
        }
    }
}